﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GlxRRSDB.ReportDataSetTableAdapters;
using GlxRRSDB;

namespace GlxRRSMain.UserControls
{
    public partial class BORevenueManagementControl : UserControl
    {
        private CinemaBORevenueViewTableAdapter x_CinemaBORevenueViewTableAdapter = new CinemaBORevenueViewTableAdapter();
        private List<ReportDataSet.CinemaBORevenueViewRow> x_ListCinemaBORevenue = null;
        ReportDataSet.CinemaBORevenueViewRow x_CinemaBORevenue = null;
        //
        private tblCinemaTableAdapter x_tblCinemaTableAdapter = new tblCinemaTableAdapter();
        private List<ReportDataSet.tblCinemaRow> x_ListCinema = null;
        //
        private tblBORevenueTableAdapter x_tblBORevenueTableAdapter = new tblBORevenueTableAdapter();
        BindingSource bs = new BindingSource(); 
        //
        private void BORevenueManagementControl_Load(object sender, EventArgs e)
        {
            initDataGridView();
            //
            bs.DataSource = ListCinemaBORevenue;
            bs.Filter = string.Format("CinemaName LIKE '*{0}*'", "nguyen");
            gridBORevenue.DataSource = ListCinemaBORevenue;
            //
            cbCinema.DataSource = ListCinema;
            cbCinema.DisplayMember = "CinemaName";
            //
            bindingBORevenueData();
        }
        public BORevenueManagementControl()
        {
            InitializeComponent();
        }
        private void initDataGridView()
        {
            initBORevenueDataGridView();
        }
        private void initBORevenueDataGridView()
        {
            gridBORevenue.AutoGenerateColumns = false;
            gridBORevenue.ReadOnly = true;

            DataGridViewTextBoxColumn c01 = new DataGridViewTextBoxColumn();
            c01.DataPropertyName = "BORevenueID";
            c01.HeaderText = "ID";

            DataGridViewTextBoxColumn c02 = new DataGridViewTextBoxColumn();
            c02.DataPropertyName = "CinemaName";
            c02.HeaderText = "Cinema";

            DataGridViewTextBoxColumn c03 = new DataGridViewTextBoxColumn();
            c03.DataPropertyName = "Admission";
            c03.HeaderText = "Admission";

            DataGridViewTextBoxColumn c04 = new DataGridViewTextBoxColumn();
            c04.DataPropertyName = "BO";
            c04.HeaderText = "BO";

            DataGridViewTextBoxColumn c05 = new DataGridViewTextBoxColumn();
            c05.DataPropertyName = "Date";
            c05.HeaderText = "Date";
            //
            gridBORevenue.Columns.Add(c01);
            gridBORevenue.Columns.Add(c02);
            gridBORevenue.Columns.Add(c03);
            gridBORevenue.Columns.Add(c04);
            gridBORevenue.Columns.Add(c05);
        }

        private void bindingBORevenueData()
        {
            if (CurrentCinemaBORevenue != null)
            {
                txtBORevenueID.Text = CurrentCinemaBORevenue.BORevenueID.ToString();
                cbCinema.Text = CurrentCinemaBORevenue.CinemaName;
                dpickerDateBORevenue.Value = CurrentCinemaBORevenue.Date;
                txtAdmission.Text = CurrentCinemaBORevenue.Admission.ToString();
                txtBORevenue.Text = CurrentCinemaBORevenue.BO.ToString();
            }
        }

        public List<ReportDataSet.CinemaBORevenueViewRow> ListCinemaBORevenue
        {
            get
            {
                if (x_ListCinemaBORevenue == null)
                {
                    x_ListCinemaBORevenue = x_CinemaBORevenueViewTableAdapter.GetData().ToList();
                }
                return x_ListCinemaBORevenue;
            }
            set
            {
                x_ListCinemaBORevenue = value;
            }
        }

        public ReportDataSet.CinemaBORevenueViewRow CurrentCinemaBORevenue
        {
            get
            {
                if (x_CinemaBORevenue == null)
                {
                    if (gridBORevenue.CurrentRow != null)
                    {
                        x_CinemaBORevenue = ListCinemaBORevenue[gridBORevenue.CurrentRow.Index];
                    }
                }
                return x_CinemaBORevenue;
            }
            set
            {
                x_CinemaBORevenue = value;
            }
        }

        public List<ReportDataSet.tblCinemaRow> ListCinema
        {
            get
            {
                if (x_ListCinema == null)
                {
                    x_ListCinema = x_tblCinemaTableAdapter.GetData().ToList();
                }
                return x_ListCinema;
            }
            set
            {
                x_ListCinema = value;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                ((DataTable)gridBORevenue.DataSource).DefaultView.RowFilter = string.Format("CinemaName like '%{0}%'", "nguyen".Replace("'", "''")); 

            }
            catch (Exception) { } 

            txtAdmission.Text = "";
            txtBORevenue.Text = "";
            dpickerDateBORevenue.Value = DateTime.Now;
        }

        private void gridBORevenue_SelectionChanged(object sender, EventArgs e)
        {
            x_CinemaBORevenue = null;
            bindingBORevenueData();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            int CinemaID = ((ReportDataSet.tblCinemaRow)cbCinema.SelectedItem).ID;
            int AdmitTarget = int.Parse(txtAdmission.Text);
            long BOTarget = long.Parse(txtBORevenue.Text);
            DateTime FromDate = dpickerDateBORevenue.Value;
            //
            ReportDataSet.tblTargetDataTable t = new ReportDataSet.tblTargetDataTable();
            t.DefaultView.RowFilter = "";
            x_tblBORevenueTableAdapter.Insert(CinemaID, AdmitTarget, BOTarget, FromDate);

            ListCinemaBORevenue = null;
            gridBORevenue.DataSource = ListCinemaBORevenue;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            int targetID = int.Parse(txtBORevenueID.Text);
            //
            ReportDataSet.tblBORevenueRow current = x_tblBORevenueTableAdapter.GetData()
                .Where(kvp => kvp.ID == targetID)
                .First();
            //
            int CinemaID = ((ReportDataSet.tblCinemaRow)cbCinema.SelectedItem).ID;
            int AdmitTarget = int.Parse(txtAdmission.Text);
            int BOTarget = int.Parse(txtBORevenue.Text);
            DateTime FromDate = dpickerDateBORevenue.Value;
            //
            current.CinemaID = CinemaID;
            current.Admission = AdmitTarget;
            current.BO = BOTarget;
            //
            current.Date = dpickerDateBORevenue.Value;

            x_tblBORevenueTableAdapter.Update(current);

            ListCinemaBORevenue = null;
            gridBORevenue.DataSource = ListCinemaBORevenue;
        }
    }
}
